package com.ddlaw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ddlaw.entity.TLawKnowledge;
import com.ddlaw.entity.TLawKnowledgeDetail;
import com.ddlaw.entity.TLawKnowledgeType;
import com.ddlaw.service.ContractsService;
import com.ddlaw.service.KnowledgeService;
import com.ddlaw.service.LawyerService;
import com.ddlaw.service.UserService;

/**
 * 
 *@author YunLong
 *@time 下午8:45:25
 */
@Controller
@RequestMapping(value = "/knowledge")
public class KonwledgeController {
	
	@Resource
	UserService userService;
	@Resource
	LawyerService lawyerService;
	@Resource
	ContractsService contractsService;
	@Resource
	KnowledgeService knowledgeService;
	
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String productListV(Model model) {
		List<TLawKnowledgeDetail> details = knowledgeService.getAllKnowledge();
		List<TLawKnowledge> knowledges = knowledgeService.getAllType();
		model.addAttribute("details", details);
		model.addAttribute("knowledges", knowledges);
		return "knowledge/index";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addProductV(Model model) {
		List<TLawKnowledge> type = knowledgeService.getAllType();
		model.addAttribute("type", type);
		return "knowledge/add";
	}
	
	@RequestMapping(value = "type/{typeId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> type(@PathVariable(value = "typeId") int typeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<TLawKnowledgeType> types = knowledgeService.getType(typeId);
		result.put("smallType", types);
		return result;
	}
	
	/***
	 * 添加法律知识
	 * @param detail 法律知识详情
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addProduct(TLawKnowledgeDetail detail) {
		Map<String, Object> result = new HashMap<String, Object>();
		detail.setContent(detail.getContent().replaceAll(" ", "&nbsp;&nbsp;").replaceAll("\r", "<br>"));
		int rs = knowledgeService.addKnowledge(detail);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "添加成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "系统异常！请重试！");
		return result;
	}
	
	
	@RequestMapping(value = "remove/{knowledgeId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> remove(@PathVariable(value = "knowledgeId") int knowledgeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		int rs = knowledgeService.delKnowledgeById(knowledgeId);
		if (rs > 0) {
			result.put("code", 200);
			result.put("msg", "删除成功！");
			return result;
		}
		result.put("code", -1);
		result.put("msg", "删除失败!请稍后再试！");
		return result;
	}
	@RequestMapping(value = "search", method = RequestMethod.POST)
	public String search(int lawknowId,Model model) {
		List<TLawKnowledgeDetail> details = null;
		if (lawknowId == 0) {
			details = knowledgeService.getAllKnowledge();
		}else{
			details = knowledgeService.getById(lawknowId);
		}
		List<TLawKnowledge> knowledges = knowledgeService.getAllType();
		model.addAttribute("details", details);
		model.addAttribute("knowledges", knowledges);
		model.addAttribute("lawknowId", lawknowId);
		return "knowledge/index";
	}
}
